//南航明珠商城-添加商品到限时统一折扣活动.js

	function addSelectedProduct(v){
       var ids =window.parent.document.getElementById("saleProducIds").value;
       /* var ids="";
		$("[id^=ids]:checked").each(function(){
			var id=$(this).attr("value");
			alert(id);
			ids+=id+",";
		}) */
	
		var p=window.parent;
		var temp=p.document.getElementById("productTable").childNodes;
		var temp1
		for(var i=0;i<temp.length;i++){
			if(temp[i].nodeType == 1)
			     temp1=temp[i];
		}
		
		//删除
		var temp2=temp1.getElementsByTagName("tr");
		for(var i=1;i<temp2.length;i++){
			//temp1.removeChild(temp2[i]);
		}
		
		var pr=$("#specs_table").find("tr");
		for(var i=1;i<pr.length;i++){
			var tFlag=pr.eq(i).find("input").prop("checked");
			
			var tr=document.createElement("tr");
			tr.setAttribute("align","center");
			
			var ieTr='<tr align="center">';
			
			if(tFlag){
				var tds=pr.eq(i).find("td");
				var str='';
				var t1='';
				var j=1;
				var ie=false;
				if("ActiveXObject" in window || !!window.ActiveXObject){
					j=1;
					ie=true;
				}
				var ieTd='';
				for(;j<tds.length;j++){
					var text=tds.eq(j).text();
					text=text.replace(/\s+/g,"").replace(/\n+/gm,"");					
					if(j == 1){
						t1=text;
						tr.appendChild(getTdNode(text,t1,ids));
						ieTd+='<td style="width: 100px;">'+text+'<input type="hidden" id="'+t1+'" name="pro_id"></td>';
					}else{
						tr.appendChild(getTdNode(text));
						ieTd+='<td style="width: 100px;">'+text+'</td>';
					}
				}
				tr.setAttribute('id',"sale" + t1);
				ieTr=ieTr+ieTd;
				ieTr+='<td style="width: 100px;"><a class="del-c">删除</a></td>';
				ieTr+='</tr>';
				var td=document.createElement("td");
				td.style.cssText="width:100px;";
				var a=document.createElement("a");
				a.innerHTML='删除';
				a.className='del-c';
				a.setAttribute('id',t1);
				td.appendChild(a);
				tr.appendChild(td);
				var f=checkTrNodeExst(temp2,t1);
				if(checkIE()){
					if(!f){
						temp1.innerHTML=temp1.innerHTML.toString()+ieTr;
					}
						
				}else{
					if(!f)
						temp1.appendChild(tr);
				}
			}
		}
		
		var p2=p.document.getElementById("productTable");
		var pL=temp1.getElementsByTagName("tr").length;
		p.document.getElementById("product_id").innerHTML="您已选择"+pL+"款商品";
       	if(v == undefined){
	    	if(ids==""){
	         alert("请选择需要添加的商品!");
	         return false;
	        }
	    	var index = parent.layer.getFrameIndex(window.name);
	        parent.location.reload=null;
	    	parent.layer.close(index);
      	}
      	     
	}
$(function(){
		   var selectedProductIds = parent.document.getElementById("saleProducIds").value;
		   var productIdArr = selectedProductIds.split(",");
		   var j = 0 ;
		   for(var i=0;i<productIdArr.length;i++){
			 if($("#chk"+productIdArr[i]).length > 0){
				 $("#chk"+productIdArr[i]).prop("checked","checked");
				 j++;
			 }
			 if(j == 10){
				 $("#chkAll2").prop("checked","checked");
			 }
		   }
	});
	
function check_expire_product(){
	var selectedProductIds = parent.document.getElementById("saleProducIds").value;
	if(selectedProductIds == ""){
		layer.alert("请选择商品！");
		return false;
	}
	
}
//checkbox 选中或者不选中商品
function changeSelectProduct(e){
	if(e.checked){
		parent.addProduct(e.value);
	}else{
		parent.deleteProduct(e.value);
	}
}
//全选
function allSelect(){
	if($("#chkAll2").prop("checked")){
		$("input[name='chk']").each(function(){
			$(this).prop("checked","checked");
			parent.addProduct(this.value);
		});
	}else{
		$("input[name='chk']").each(function(){
			$(this).prop("checked","");
			parent.deleteProduct(this.value);
		});
	}
}

function search(){
	     var productId=  document.getElementsByName("prodId")[0].value
	     if(productId!=''&&isNaN(productId)){ 
	         layer.layer.alert("输入的商品编号有误，请重新输入！");
	         return  ;
	    }
	    document.getElementsByName("prodId")[0].value=(document.getElementsByName("prodId")[0].value).trim(); 
	    document.getElementsByName("proName")[0].value=(document.getElementsByName("proName")[0].value).trim(); 
    	addSelectedProduct(true);
        $("#page").val("1");
        $(query_product_list_form).submit();
	 }	
function toIsEmpty(){
   document.getElementsByName("proName")[0].value= "";
   document.getElementsByName("prodId")[0].value= "";
   document.getElementsByName("merchantQo.id")[0].value= "";
}
function getTdNode(str,id,ids){
    var input=document.createElement("input"); 
   
	input.type='hidden';
	
	var td=document.createElement("td");
	td.style.cssText="width:100px;";
	
	if(checkIE()){
		td.innerText=str;
	}else{
		td.innerHTML=str;
	}
	if(id != undefined){
		input.id=id;
	    input.name='pro_id';
		/* input.name=ids; */
		td.appendChild(input);
		
	}
	return td;
}

function checkTrNodeExst(trs,e){
	for(var i=0;i<trs.length;i++){
		var text=trs[i].getElementsByTagName("td")[0].innerHTML;
		if(checkIE()){
			text=trs[i].getElementsByTagName("td")[0].innerText;
		}
		text=text.replace(/(<[^>]*>)|(<\/[^>]*>)/gm,"");
		text=text.replace(/\s+/g,"").replace(/\n+/gm,"");
		if(text == e)
			return true;
	}
	
	return false;
}

function checkIE(){
	if("ActiveXObject" in window || !!window.ActiveXObject){
				return true;
			}else{
				return false;
			}
}